package is.ru.honn.rupin.data;

import is.ru.honn.rupin.domain.Follower;
import is.ru.honn.rupin.domain.User;
import is.ruframework.data.RuData;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: echo
 * Date: 11/18/12
 * Time: 11:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class FollowerData extends RuData implements FollowerDataGateway {

    @Override
    public String add(String follower, String followee) {
        SimpleJdbcInsert insert =
                new SimpleJdbcInsert(getDataSource())
                        .withTableName("ru_followers");

        Map<String, Object> parameters = new HashMap<String, Object>(2);
        parameters.put("follower", follower);
        parameters.put("followee", followee);

        insert.execute(parameters);
        return followee;

    }

    @Override
    public List<Follower> getFollowers(String username) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
        List<Follower> followers = (List<Follower>)jdbcTemplate.query(
                "select * from ru_followers where followee=?", new FollowerRowMapper(), username);


        return followers;
    }

    public List<Follower> getFollowees(String username) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
        List<Follower> followers = (List<Follower>)jdbcTemplate.query(
                "select * from ru_followers where follower=?", new FollowerRowMapper(), username);


        return followers;
    }
}
